<template>
  <div class="comment-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <crumbs>评论管理</crumbs>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column label="文章标题" width="350" prop="title"></el-table-column>
        <el-table-column label="总评论数" prop="total_comment_count"></el-table-column>
        <el-table-column label="粉丝评论数" prop="fans_comment_count"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.comment_status ? '已打开' : '已关闭' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
           <template slot-scope="scope">
            <el-button @click="isComment(scope.row)" v-if="!scope.row.comment_status" type="success" size="small">打开评论</el-button>
            <el-button @click="isComment(scope.row)" v-else type="danger" size="small">关闭评论</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px"
        background
        @size-change="changeSize"
        @current-change="changePage"
        :current-page="reqParams.page"
        :page-sizes="[10, 15, 20, 25, 30]"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// 获取评论数据
import { articleList as CommentList, commentState } from '@/api/article'
export default {
  name: 'Comment',
  data () {
    return {
      reqParams: {
        page: 1,
        per_page: 10,
        response_type: 'comment' // 传 comment 返回用于评论管理的字段
      },
      total: 0,
      tableData: []
    }
  },
  mounted () {
    this.getCommentList()
  },
  methods: {
    // 获取评论列表
    async getCommentList () {
      const { data: { data } } = await CommentList(this.reqParams)
      // console.log(data)
      this.tableData = data.results
      this.total = data.total_count
    },
    // 是否打开评论
    isComment (row) {
      console.log(row)
      const txt = row.comment_status ? '关闭评论' : '打开评论'
      console.log(!row.comment_status)
      this.$confirm(`此操作将永久${txt}, 是否继续?`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await commentState({ id: row.id.toString(), allow_comment: !row.comment_status })
        this.$message.success(txt + '成功!')
        console.log(res)
        row.comment_status = res.data.data.allow_comment
      }).catch(() => {
      })
    },
    // 分页跳转
    changePage (curPage) {
      this.reqParams.page = curPage
      this.getCommentList()
    },
    // 每页条数
    changeSize (curSize) {
      this.reqParams.page = 1
      this.reqParams.per_page = curSize
      this.getCommentList()
    }
  }
}
</script>

<style lang="less" scoped>

</style>
